package org.example.javawebday1.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.javawebday1.mapper.DeptMapper;
import org.example.javawebday1.pojo.Dept;
import org.example.javawebday1.pojo.Result;
import org.example.javawebday1.service.DeptsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class DeptsServiceImpl implements DeptsService
{

    @Autowired
    private DeptMapper DeptMapper;
    @Override
    public Result getAllDepts() {

        try {
            List<Dept> depts = DeptMapper.getAllDepts();
            log.info("获取部门列表成功");
            return Result.success(depts);
        }
        catch (Exception e)
        {
            log.error("获取部门列表失败");
            return Result.error("获取部门列表失败");
        }

    }

    @Override
    public Result deleteDeptById(Integer id) {
        try {
             int i = DeptMapper.deleteDeptById( id);
            log.info("成功删除{}个部门",i);
            return Result.success();
        }
        catch (Exception e)
        {
            log.error("删除对应部门失败");
            return Result.error("删除对应部门失败");
        }
    }

    @Override
    public Result addDept(Dept dept) {
        LocalDateTime now = LocalDateTime.now();
        dept.setCreateTime(now);
        dept.setUpdateTime(now);
        System.out.println("当前工作目录: " + System.getProperty("user.dir"));
        try {
            int i = DeptMapper.addDept(dept);
            log.info("成功添加{}个部门,部门信息是{}",i,dept);
            return Result.success();
        }
        catch (Exception e)
        {
            log.error("新增部门失败",e);
            return Result.error("添加部门失败");
        }

    }

    @Override
    public Result addDepts(List<Dept> depts) {
        
        try {
            depts.forEach(dept -> {
                LocalDateTime now = LocalDateTime.now();
                dept.setCreateTime(now);
                dept.setUpdateTime(now);
                DeptMapper.addDept(dept);
            });
            return Result.success();
        }
        catch (Exception e)
        {
            log.error("新增部门失败",e);
            return Result.error("添加部门失败");
        }

    }

    @Override
    public Result getDeptById(Integer id) {
        try {
            Dept dept = DeptMapper.getDeptById(id);
            return Result.success(dept);
        } catch (Exception e) {
            log.error("获取部门信息失败",e);
            return Result.error("获取部门信息失败");
        }
    }

    @Override
    public Result updateDept(Dept dept) {
        LocalDateTime now = LocalDateTime.now();
        dept.setUpdateTime(now);
        try {
            int i = DeptMapper.updateDept(dept);
            System.out.println(i);
            return Result.success();
        }
        catch (Exception e)
        {
            System.out.println(e);
            return Result.error("更新部门信息失败");
        }
    }


}
